package com.gewu.bookeeping.View.activity;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.gewu.bookeeping.MyDb.bean.ALIBeanIn;
import com.gewu.bookeeping.MyDb.bean.ALiBeanOut;
import com.gewu.bookeeping.MyDb.bean.MesBeanIn;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanIn;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanOut;
import com.gewu.bookeeping.MyDb.dao.ALiDaoInPerson;
import com.gewu.bookeeping.MyDb.dao.ALiDaoOutPerson;
import com.gewu.bookeeping.MyDb.dao.MesInPerson;
import com.gewu.bookeeping.MyDb.dao.WeChtDaoInPerson;
import com.gewu.bookeeping.MyDb.dao.WeChtDaoOutPerson;
import com.gewu.bookeeping.R;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;

import java.util.ArrayList;
import java.util.List;

public class fenxiActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private PieChart wpin;
    private PieChart apin;
    private PieChart zpin;
    List<MesBeanIn> quer5;
    List<WeChtBeanOut> quer;
    List<WeChtBeanIn> quer1;
    List<ALiBeanOut> quer2;
    List<ALIBeanIn> quer3;
    List<MesBeanIn> quer4;
    private List<PieEntry> strings = new ArrayList<>();
    private List<PieEntry> strings2 = new ArrayList<>();
    private List<PieEntry> strings3 = new ArrayList<>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fenxi);
        initView();
        getShuju();
        setPinone();
        setPinone1();
        setPinone2();
    }

    private void setPinone() {
        strings.add(new PieEntry(quer.size()+quer1.size(),"微信支出收入"));
        strings.add(new PieEntry(1000,"总支出"));
        PieDataSet dataSet = new PieDataSet(strings, "微信支出/收入占比总支出的图");
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(getResources().getColor(R.color.colorAccent));
        colors.add(getResources().getColor(R.color.selectitem));
        dataSet.setColors(colors);
        PieData pieData = new PieData(dataSet);
        pieData.setDrawValues(true);
        wpin.setData(pieData);
        wpin.invalidate();
    }

    private void setPinone1() {
        strings3.add(new PieEntry(quer4.size(),"总支出"));
        strings3.add(new PieEntry(10000,"年轻人"));
        PieDataSet dataSet = new PieDataSet(strings3, "总支出占比年轻人总支出的图");
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(getResources().getColor(R.color.colorAccent));
        colors.add(getResources().getColor(R.color.selectitem));
        dataSet.setColors(colors);
        PieData pieData = new PieData(dataSet);
        pieData.setDrawValues(true);
        apin.setData(pieData);
        apin.invalidate();
    }

    private void setPinone2() {
        strings2.add(new PieEntry(quer2.size()+quer3.size(),"支付宝支出收入"));
        strings2.add(new PieEntry(1000,"总支出"));
        PieDataSet dataSet = new PieDataSet(strings2, "支付宝支出/收入占比总支出的图");
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(getResources().getColor(R.color.colorAccent));
        colors.add(getResources().getColor(R.color.selectitem));
        dataSet.setColors(colors);
        PieData pieData = new PieData(dataSet);
        pieData.setDrawValues(true);
        zpin.setData(pieData);
        zpin.invalidate();
    }

    private void getShuju() {
        MesInPerson mesInPerson6 = new MesInPerson(this);
        quer5 = mesInPerson6.quer();
        for (int i = 0; i < quer5.size(); i++) {
            mesInPerson6.delect(quer5.get(i).getId());
        }

        WeChtDaoOutPerson weChtDaoOutPerson = new WeChtDaoOutPerson(this);
        quer = weChtDaoOutPerson.quer();
        if (quer != null) {
            for (int i = 0; i < quer.size(); i++) {
                MesInPerson mesInPerson = new MesInPerson(this);
                MesBeanIn mesBeanIn = new MesBeanIn();
                mesBeanIn.setText(quer.get(i).getText());
                mesBeanIn.setTime(quer.get(i).getTime());
                mesBeanIn.setType(quer.get(i).getType());
                mesBeanIn.setMoney(quer.get(i).getMoney());
                mesInPerson.add(mesBeanIn);
            }
        }

        WeChtDaoInPerson weChtDaoInPerson = new WeChtDaoInPerson(this);
        quer1 = weChtDaoInPerson.quer();
        if (quer1 != null) {
            for (int i = 0; i < quer1.size(); i++) {
                MesInPerson mesInPerson = new MesInPerson(this);
                MesBeanIn mesBeanIn = new MesBeanIn();
                mesBeanIn.setText(quer1.get(i).getText());
                mesBeanIn.setTime(quer1.get(i).getTime());
                mesBeanIn.setType(quer1.get(i).getType());
                mesBeanIn.setMoney(quer1.get(i).getMoney());
                mesInPerson.add(mesBeanIn);
            }
        }

        ALiDaoOutPerson aLiDaoOutPerson = new ALiDaoOutPerson(this);
        quer2 = aLiDaoOutPerson.quer();
        if (quer2 != null) {
            for (int i = 0; i < quer2.size(); i++) {
                MesInPerson mesInPerson = new MesInPerson(this);
                MesBeanIn mesBeanIn = new MesBeanIn();
                mesBeanIn.setText(quer2.get(i).getText());
                mesBeanIn.setTime(quer2.get(i).getTime());
                mesBeanIn.setType(quer2.get(i).getType());
                mesBeanIn.setMoney(quer2.get(i).getMoney());
                mesInPerson.add(mesBeanIn);
            }
        }

        ALiDaoInPerson aLiDaoInPerson = new ALiDaoInPerson(this);
        quer3 = aLiDaoInPerson.quer();
        if (quer3 != null) {
            for (int i = 0; i < quer3.size(); i++) {
                MesInPerson mesInPerson = new MesInPerson(this);
                MesBeanIn mesBeanIn = new MesBeanIn();
                mesBeanIn.setText(quer3.get(i).getText());
                mesBeanIn.setTime(quer3.get(i).getTime());
                mesBeanIn.setType(quer3.get(i).getType());
                mesBeanIn.setMoney(quer3.get(i).getMoney());
                mesInPerson.add(mesBeanIn);
            }
        }

        MesInPerson mesInPerson = new MesInPerson(this);
        quer4 = mesInPerson.quer();
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);

        toolbar.setTitle("账户分析");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        wpin = (PieChart) findViewById(R.id.wpin);
        apin = (PieChart) findViewById(R.id.apin);
        zpin = (PieChart) findViewById(R.id.zpin);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}